package com.bm.util;

import java.sql.*;

public class JdbcUtil {
    private static String url = "jdbc:mysql://localhost:3306/db_college_data?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF8&autoReconnect=true&failOverReadOnly=false";
    private static String password = "123asdzxc";
    private static String user = "root";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        final Connection conn= DriverManager.getConnection(url, user, password);
        return conn;
    }

    /**
     * 释放资源
     *
     * @param conn 数据库连接对象
     * @param stmt 执行工具
     * @param rs 结果集对象
     */
    public static void release(Connection conn , Statement stmt , ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            release(conn , stmt);
        }
    }

    /**
     * 释放资源
     *
     * @param conn 数据库连接对象
     * @param stmt 执行工具
     */
    public static void release(Connection conn , Statement stmt ) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}